unit uConfig;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Mask, KMaskEdit, KEdit, ExtCtrls, Buttons;

type
  TfrmConfig = class(TForm)
    btnOk: TBitBtn;
    btnCancel: TBitBtn;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    KEdit1: TKEdit;
    GroupBox2: TGroupBox;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    GroupBox4: TGroupBox;
    ComboBox1: TComboBox;
    Label2: TLabel;
    ComboBox2: TComboBox;
    Label3: TLabel;
    ComboBox3: TComboBox;
    Label4: TLabel;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    procedure RadioButton1Click(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure btnOkClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure KEdit1SpeedButtonClick(Sender: TObject; var Accept: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
  Function ShowFormConfig : Boolean;
var
  frmConfig: TfrmConfig;

implementation
{$WARN UNIT_PLATFORM OFF}
uses UKStd, Registry,FileCtrl,utils, uDataModule,uMain;

{$R *.dfm}

Function ShowFormConfig : Boolean;
begin

  frmConfig := TfrmConfig.Create(Application);
  with frmConfig do
  try
    Result := ShowModal=mrOk;
  finally
    Free;
  end;
end;


procedure TfrmConfig.RadioButton1Click(Sender: TObject);
begin
  KEdit1.SetFocus;
end;

procedure TfrmConfig.btnCancelClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmConfig.btnOkClick(Sender: TObject);
begin

  with TRegistry.Create do
  try
    RootKey := HKEY_LOCAL_MACHINE;
    if OpenKey('\Software\Udonhos\Tbscan', True) then begin
      WriteInteger('ScanType', IIf(RadioButton1.Checked, 0, 1));
      WriteInteger('ShowPicture', IIf(CheckBox1.Checked, 1, 0));
      WriteInteger('ShowUI', IIf(CheckBox2.Checked, 1, 0));
      WriteInteger('Mode',ComboBox1.ItemIndex);
      WriteInteger('Page',ComboBox2.ItemIndex);
      WriteInteger('Dots',strtoint(ComboBox3.Items.Strings[ComboBox3.ItemIndex]));
      CloseKey;
    end;
  finally
    Free;
  end;

  if DoneQuery('UPDATE MCODE SET CODEREP = '''+KEdit1.Text+''' WHERE CODE =''TB'' ') then
  Close;

  showPicture := IIf(CheckBox1.Checked,1,0);
  showUI      := IIf(CheckBox2.Checked,1,0);
  scan        := IIf(RadioButton1.Checked, 0, 1);
  mode        := ComboBox1.ItemIndex;
  page        := ComboBox2.ItemIndex;
  dots        := strtoint(ComboBox3.Items.Strings[ComboBox3.ItemIndex]);

end;

procedure TfrmConfig.FormShow(Sender: TObject);
begin

  KEdit1.Text := GetConfigValue('CODEREP','TB');

  with TRegistry.Create do
  try
    RootKey := HKEY_LOCAL_MACHINE;
    if OpenKey('\Software\Udonhos\Tbscan', False) then
    begin
      if ReadInteger('ScanType')=0 then
        RadioButton1.Checked := True
      else
        RadioButton2.Checked := True;
      if ReadInteger('ShowPicture')=1  then
        CheckBox1.Checked := True
      else
        CheckBox1.Checked := False;
        if ReadInteger('ShowUI')=1  then
        CheckBox2.Checked := True
      else
        CheckBox2.Checked := False;
      ComboBox1.ItemIndex := ReadInteger('Mode');
      ComboBox2.ItemIndex := ReadInteger('Page');
      ComboBox3.ItemIndex := ComboBox3.Items.IndexOf(inttostr(ReadInteger('Dots')));
      CloseKey;
    end;
  finally
    Free;
  end;
end;

procedure TfrmConfig.KEdit1SpeedButtonClick(Sender: TObject;
  var Accept: Boolean);
  var Dir: string;
begin
        Dir := TKEdit(Sender).Text;
        if SelectDirectory(Dir, [sdAllowCreate, sdPerformCreate, sdPrompt], 0) then
        begin
        TKEdit(Sender).text := '\\'+LocalIP+COpy(Dir,POS(':',Dir)+1,(Length(Dir)-POS(':',Dir)))+'\';
        end;
end;

end.
